package com.example.gyt.controller;

import com.example.gyt.pojo.Commodity;
import com.example.gyt.pojo.Newsl;
import com.example.gyt.pojo.PageResult;
import com.example.gyt.pojo.ProductOrder;
import com.example.gyt.service.ProductOrderService;
import com.example.gyt.service.UserService;
import com.example.gyt.util.DateUtil;
import com.example.gyt.util.ResultUtil;
import com.example.gyt.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class ProductOrderJsonController {

    @Autowired
    private ProductOrderService productOrderService;

    //添加订单方法
    @ResponseBody
    @RequestMapping(value = "/member/addOrder",method = RequestMethod.POST)
    public ResultUtil addOrder(@Validated @RequestBody ProductOrder productOrder, BindingResult result){
        try {

            //是否为空检验
            if(StringUtils.isEmpty(productOrder.getBuyerId())){
                return ResultUtil.failure("买家id不能为空");
            }
            if(StringUtils.isEmpty(productOrder.getSellerId())){
                return ResultUtil.failure("卖家id不能为空");
            }
            if(StringUtils.isEmpty(productOrder.getOrderStatus())){
                return ResultUtil.failure("订单状态不能为空");
            }

            productOrder.setId(UuidUtil.getUUID());

            //获取到当前时间
            productOrder.setCreateTime(DateUtil.getCurrentTime());

            int count = productOrderService.insertSelective(productOrder);
            if (count == 0){
                return ResultUtil.success("添加订单失败");
            }

            return ResultUtil.success("添加订单信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }

    }

    //删除一条新闻
    @ResponseBody
    @RequestMapping(value = "/member/deleteProductOrder",method = RequestMethod.POST)
    public ResultUtil deleteProductOrder(@Validated @RequestBody ProductOrder productOrder, BindingResult result){
        try {

            if(StringUtils.isEmpty(productOrder.getId())){
                return ResultUtil.failure("订单ID不能为空");
            }

            int count = productOrderService.deleteByPrimaryKey(productOrder.getId());
            if (count == 0){
                return ResultUtil.failure("删除订单失败");
            }

            return ResultUtil.success("删除订单成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //订单ID查信息
    @ResponseBody
    @RequestMapping(value = "/member/queryProductOrder",method = RequestMethod.POST)
    public ResultUtil queryProductOrder(@Validated @RequestBody ProductOrder productOrder, BindingResult result){
        try {

            if(StringUtils.isEmpty(productOrder.getId())){
                return ResultUtil.failure("订单ID不能为空");
            }

            ProductOrder productOrder1 = productOrderService.selectByPrimaryKey(productOrder.getId());
            if (productOrder1 == null){
                return ResultUtil.failure("获取订单信息失败");
            }

            return ResultUtil.success(productOrder1);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //修改订单信息
    @ResponseBody
    @RequestMapping(value = "/member/updateProductOrder",method = RequestMethod.POST)
    public ResultUtil updateProductOrder(@Validated @RequestBody ProductOrder productOrder, BindingResult result){
        try {
            if(StringUtils.isEmpty(productOrder.getId())){
                return ResultUtil.failure("订单ID不能为空");
            }

            /*if(StringUtils.isEmpty(user.getUserName())){
                user.setUserName(null);
            }

            if(StringUtils.isEmpty(user.getUserBrithday())){
                user.setUserBrithday(null);
            }

            if(StringUtils.isEmpty(user.getUserAddress())){
                user.setUserAddress(null);
            }

            if(StringUtils.isEmpty(user.getUserTel())){
                user.setUserTel(null);
            }*/

            //更新修改时间
            productOrder.setUpdateTime(DateUtil.getCurrentTime());

            int count = productOrderService.updateByPrimaryKeySelective(productOrder);
            if (count == 0){
                return ResultUtil.failure("修改订单信息失败");
            }

            return ResultUtil.success("修改订单信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }

    //集合于订单分页查询，所有订单查询，订单类型查询，订单名字模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allProductOrderList",method = RequestMethod.POST)
    public ResultUtil allProductOrderList(@Validated @RequestBody ProductOrder productOrder, BindingResult result){
        try {

            List<ProductOrder> productOrder1 = productOrderService.getAllProductOrderList(productOrder);
            if (productOrder1.size() == 0){
                return ResultUtil.failure("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = productOrderService.queryCountLike(productOrder);

            //加载实体将数据返回给用户
            PageResult pageResult = new PageResult();
            if (productOrder.getPageResult() != null){
                pageResult.setCurrentPage(productOrder.getPageResult().getCurrentPage());
                pageResult.setPageSize(productOrder.getPageResult().getPageSize());

            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(productOrder1);

            return ResultUtil.success(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failure("操作失败");
        }
    }
}
